package org.nch.videoLauncher.domain;

import org.nch.videoLauncher.domain.IPlayable;

import java.util.List;
import java.util.ArrayList;
import java.io.File;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 14, 2008
 * Time: 1:22:26 PM
 * To change this template use File | Settings | File Templates.
 */
public class VideoItem extends BaseEntity implements Comparable, IPlayable {

//    private Integer id;
//    private Integer version;
    private String title;
    private String filename;
    private String url;
    private String description;
    private String keywords;
    private String runTime;

    private TVInfo tvInfo;
    private SportsEvent sportsEvent;
    private IMDBInfo imdbInfo;

    private List<VideoFile> files = new ArrayList<VideoFile>();

    // constructors

    public VideoItem() {

    }

    public VideoItem(String title, String filename) {
        this.setTitle(title);
        this.setFilename(filename);
        this.addFile(filename);
    }

    // accessor methods

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {

        this.title = title;

        if (this.getImdbInfo() != null){
            if (imdbInfo.getTitle() != null && !imdbInfo.getTitle().equals("")){
                this.title = imdbInfo.getTitle();
            }
        }

        if (this.getTvInfo() != null){
            if (tvInfo.getShow() != null && !tvInfo.getShow().equals("")){
                StringBuilder newTitle = new StringBuilder();
                newTitle.append(tvInfo.getShow());
                if (tvInfo.getSeason() != null){
                    newTitle.append(" Season ");
                    newTitle.append(String.valueOf(tvInfo.getSeason()));
                }
                if (tvInfo.getEpisode() != null){
                    newTitle.append(" Episode ");
                    newTitle.append(String.valueOf(tvInfo.getEpisode()));
                }
                this.title = newTitle.toString();
            }
        }

        if (this.getSportsEvent() != null){
            if (sportsEvent.getTeam() != null && !sportsEvent.getTeam().equals("")){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
                this.title = sportsEvent.getTeam() + ", " + sportsEvent.getSeason() + ", " + sportsEvent.getCompetition() + ", " + formatter.format(sportsEvent.getDate());
            }
        }

    }

    public List<VideoFile> getFiles() {
        return files;
    }

    public void setFiles(List<VideoFile> files) {
        this.files = files;
    }    

    public TVInfo getTvInfo() {
        return tvInfo;
    }

    public void setTvInfo(TVInfo tvInfo) {
        this.tvInfo = tvInfo;
    }

    public SportsEvent getSportsEvent() {
        return sportsEvent;
    }

    public void setSportsEvent(SportsEvent sportsEvent) {
        this.sportsEvent = sportsEvent;
    }

    public IMDBInfo getImdbInfo() {
        return imdbInfo;
    }

    public void setImdbInfo(IMDBInfo imdbInfo) {
        this.imdbInfo = imdbInfo;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getRunTime() {
        return runTime;
    }

    public void setRunTime(String runTime) {
        this.runTime = runTime;
    }

    // file helper functions

    public void addFile(String filename){
        VideoFile vf = new VideoFile();
        vf.setVideoItem(this);
        vf.setFilename(filename);
        vf.setOrder(files.size());
        this.files.add(vf);
        if (files.size() == 1){
            this.setFilename(filename);
        }
    }

    public boolean deleteFile(VideoFile file){
        if (files.contains(file) && files.indexOf(file) != 0){
            files.remove(file);
            return true;
        }
        return false;
    }

    public boolean deleteFileAtIndex(Integer index){
        if ((files.size()-1 >= index) && index != 0){
            files.remove(index.intValue());
            return true;
        }
        return false;
    }

    public String getNextFileLocation(){

        Integer last = files.get(0).getFilename().lastIndexOf(File.separator);
        return files.get(0).getFilename().substring(0, last);
    }

    // IPlayable implementation

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public Integer getFileCount() {
        return files.size();
    }

    // utility method for copying a TV show, with duplicate file

    public VideoItem copyTvShow(){

        VideoItem newShow = new VideoItem();
        newShow.setTitle("Copy of " + this.getTitle());
        newShow.setDescription(this.getDescription());
        newShow.setKeywords(this.getKeywords());
        newShow.setUrl(this.getUrl());
        newShow.setRunTime(this.getRunTime());
        newShow.setTvInfo(this.getTvInfo());
        newShow.setImdbInfo(this.getImdbInfo());
        newShow.addFile("New file - needs to be selected");

        return newShow;
    }


// domain object validation

    public boolean isValid() {

        // check required fields

        if (title == null || title.equals(""))
            return false;

        if (filename == null || filename.equals(""))
            return false;

        return true;

    }

    // override methods

    public String toString() {
        return title;
    }

    // Comparable implementation

    public int compareTo(Object o) {
        if (o.toString().equals(this.toString())) {
            return 0;
        }

        char specifiedChar = o.toString().charAt(0);
        char currentChar = this.toString().charAt(0);

        if (specifiedChar == currentChar)
            return 0;

        if (currentChar > specifiedChar)
            return 1;

        if (specifiedChar > currentChar)
            return -1;

        return 0;

    }

}
